(function(){
var homeAccountId;
var isLoggedIn;
var subdomain = window.location.host.split('.')[0];
var sha256=function a(b){function c(a,b){return a>>>b|a<<32-b}for(var d,e,f=Math.pow,g=f(2,32),h="length",i="",j=[],k=8*b[h],l=a.h=a.h||[],m=a.k=a.k||[],n=m[h],o={},p=2;64>n;p++)if(!o[p]){for(d=0;313>d;d+=p)o[d]=p;l[n]=f(p,.5)*g|0,m[n++]=f(p,1/3)*g|0}for(b+="\x80";b[h]%64-56;)b+="\x00";for(d=0;d>8)return;j[d>>2]|=e<<(3-d)%4*8}for(j[j[h]]=k/g|0,j[j[h]]=k,e=0;ed;d++){var s=q[d-15],t=q[d-2],u=l[0],v=l[4],w=l[7]+(c(v,6)^c(v,11)^c(v,25))+(v&l[5]^~v&l[6])+m[d]+(q[d]=16>d?q[d]:q[d-16]+(c(s,7)^c(s,18)^s>>>3)+q[d-7]+(c(t,17)^c(t,19)^t>>>10)|0),x=(c(u,2)^c(u,13)^c(u,22))+(u&l[1]^u&l[2]^l[1]&l[2]);l=[w+x|0].concat(l),l[4]=l[4]+w|0}for(d=0;8>d;d++)l[d]=l[d]+r[d]|0}for(d=0;8>d;d++)for(e=3;e+1;e--){var y=l[d]>>8*e&255;i+=(16>y?0:"")+y.toString(16)}return i};
var getCookie=function(c){c+="=";for(var d=document.cookie.split(";"),b=0;b{
if(response.ok){
console.log('OK:', response);
// nw_log('Success 1');
} else {
throw new Error('Bad HTTP!')
}
}).then((j) =>{
console.log('toSUCCESS:', j);
// nw_log('Success 2');
success(j);
}).catch( (err) =>{
console.log('ERROR:', err.message);
});
}
}
/**
* Callback method to get the Auth token
*
* @param home_account_id is the id of the account to fetch the token for
*/
function getAuthToken(home_account_id){
const request = {
scopes: ["User.Read"],
account: ms.auth.getMSALAccount(home_account_id),
}
if (!request.account) {
nw_log('Silent token fetch failed: Account not found');
} else {
ms.auth.acquireTokenSilent(request).then(tokenFetchResponse).catch(err => {
nw_log("Token fetch failed with error: " , err);
});
}
}
/**
* Callback method for the MSAL initalization
*
* @param signInAccountInfo is where the user info is received
* @param errorInfo is where errors are received
*/
function MsaccountCallback(account_info) {
if (account_info) {
sendToLiveRamp(account_info.username);
}
}
function userAuthenticated(signInAccountInfo, errorInfo) {
if (response.result !== "success") {
console.error("ms.auth.initialize failed: ", response);
} else {
window.postMessage("authenticated login_success","*");
// nw_log("user authenticated");
// nw_log(signInAccountInfo);
// nw_log(errorInfo);
//if(signInAccountInfo.username!="") sendToLiveRamp(signInAccountInfo.username);
}
if (!signInAccountInfo) {
window.postMessage("NOT authenticated","*");
// nw_log("Sign in failed with error: ", errorInfo);
let msg = errorInfo.errorMessage;
return;
} else {
getAuthToken(signInAccountInfo.homeAccountId);
if(signInAccountInfo.username!="") sendToLiveRamp(signInAccountInfo.username);
}
}
/**
* @param response stores the MSAL object
*/
var response = null;
/**
* Will bootstrap the MSAL library and set the callback method
*
* @param auto_prompt will enable/disable Microsoft Edge's "Enhanced" login feature.
*/
function MSInitialize(auto_prompt){
postMessage("MSInitialize!0!","*");
window.postMessage("MSInitialize!1!","*");
parent.postMessage("MSInitialize!2!","*");
// Initialize the library
response = ms.auth.initialize({
client_id: "941dade2-38f8-4759-8a5e-9d0d9fc2a804",
login_uri: "https://myaccount."+document_domain+"/oauth/login_ms_auth",
// login_uri: "https://myaccount.dev7.newsweek.com/oauth/login_ms_auth",
callback: userAuthenticated,
auto_prompt: auto_prompt,
prompt_position: "right"
});
}
function LoadMsLoginPrompt() {
ms.auth.prompt('right', function(notification) {
const reason = notification.reason;
if (notification.type === 'display' && !notification.displayed) {
if ( reason === 'browser_not_supported' ) {
nw_log('prompt not supported in browser');
}
} else if (notification.type === 'skipped') {
if (reason === 'user_cancel') {
nw_log('user cancelled');
}
} else if (notification.type === 'dismissed') {
if (reason === 'credential_returned') {
nw_log('Got sign-in credentials');
}
}
});
}
function LoadMsLoginButton() {
if (response.result === "success") {
// Render the sign-in button with JavaScript.
ms.auth.renderSignInButton(
document.getElementById("sign-in-button-placeholder"),
{
type: "icon",
theme: "light",
}
);
}
}
/**
* Here we bootstrap the main functions and DOM elements for the
* MS One Click login to work correctly
*
* Browser detection is disabled because the Quick Auth script
* automatically does that.
*
*/
window.onload = function () {
// browser_is_edge = (function (agent) {
// switch (true) {
// case agent.indexOf("edg") > -1: return true;
// default: return false;
// }
// console.error(agent);
// })(window.navigator.userAgent.toLowerCase());
// if ( browser_is_edge ){
isLoggedIn = (getCookie('nwsubl') !== null && getCookie('nwsubl') !== '');
MSInitialize(!isLoggedIn);
// Only needed if want to see login errors:
// LoadMsLoginPrompt();
// otherwise it's already done automatically in the initialization
// if(parent!=window) {
var Span_OnClick = document.createElement("a"); // create new
Span_OnClick.setAttribute("class", "blaize-oauth blaize-oauth-google"); // set class to the a
Span_OnClick.setAttribute("id" , "sign-in-button-placeholder"); // set class to the a
var loaderWrap = document.getElementById("Span_OnClick"); // get the parent element
if(loaderWrap){
loaderWrap.appendChild(Span_OnClick); // append the new a to the parent
LoadMsLoginButton();
}
if(getCookie('nwsubl') !== null && getCookie('nwsubl') !== ''){
ms.auth.startGetCurrentAccount(MsaccountCallback);
}
// }
// }
}
})();